package com.ats.feeder.view.managedbeans.admin.${ObjectMap.OBJECT_CODE_BEAN_PATTERN};

import com.ats.feeder.model.entities.${ObjectMap.OBJECT_CODE_BEAN_PATTERN}.${ObjectMap.OBJECT_CODE_CAMLE_CASE};
import com.ats.feeder.model.entities.lookups.HesCities;
import com.ats.feeder.model.entities.lookups.HesCountries;
import com.ats.feeder.model.services.admin.${ObjectMap.OBJECT_CODE_BEAN_PATTERN}.Manage${ObjectMap.OBJECT_CODE_CAMLE_CASE}ServiceLocal;
import com.ats.feeder.model.services.common.lookups.LookupsServicesLocal;
import com.ats.feeder.view.common.FacesUtil;
import com.ats.feeder.view.common.MessageHandler;
import com.ats.util.ATSSelectItem;
import com.ats.util.ATSSelectItemList;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

/**
 *
 * @author Ali
 */

@ManagedBean(name = "update${ObjectMap.OBJECT_CODE_CAMLE_CASE}Bean")
@ViewScoped
public class Update${ObjectMap.OBJECT_CODE_CAMLE_CASE}Bean implements Serializable {

    @EJB
    Manage${ObjectMap.OBJECT_CODE_CAMLE_CASE}ServiceLocal manage${ObjectMap.OBJECT_CODE_CAMLE_CASE}Service;
    @EJB
    LookupsServicesLocal lookupsServicesLocal;
    private ${ObjectMap.OBJECT_CODE_CAMLE_CASE} ${ObjectMap.OBJECT_CODE_BEAN_PATTERN};
    private ATSSelectItemList<ATSSelectItem> countries;
    private ATSSelectItemList<ATSSelectItem> cities;

    @PostConstruct
    public void initialize() {
        String locale = FacesUtil.getLocaleLang();
        try {
            set${ObjectMap.OBJECT_CODE_CAMLE_CASE}((${ObjectMap.OBJECT_CODE_CAMLE_CASE}) FacesUtil.getRequestParameterObject("selected${ObjectMap.OBJECT_CODE_CAMLE_CASE}"));
        } catch (Exception ex) {
            MessageHandler.addGenericServiceIlligalAccessMessage(locale);
            FacesUtil.redirect("manage${ObjectMap.OBJECT_CODE_CAMLE_CASE}");
        }

        populateCountries();
        populateCities();
    }

    /**
     * @return the ${ObjectMap.OBJECT_CODE_BEAN_PATTERN}
     */
    public ${ObjectMap.OBJECT_CODE_CAMLE_CASE} get${ObjectMap.OBJECT_CODE_CAMLE_CASE}() {
        return ${ObjectMap.OBJECT_CODE_BEAN_PATTERN};
    }

    /**
     * @param ${ObjectMap.OBJECT_CODE_BEAN_PATTERN} the ${ObjectMap.OBJECT_CODE_BEAN_PATTERN} to set
     */
    public void set${ObjectMap.OBJECT_CODE_CAMLE_CASE}(${ObjectMap.OBJECT_CODE_CAMLE_CASE} ${ObjectMap.OBJECT_CODE_BEAN_PATTERN}) {
        this.${ObjectMap.OBJECT_CODE_BEAN_PATTERN} = ${ObjectMap.OBJECT_CODE_BEAN_PATTERN};
    }

    private void populateCountries() {
        short currentLangId = FacesUtil.getLocaleLangId();
        
        List<HesCountries> _countries = lookupsServicesLocal.getAllCountries();
        ATSSelectItemList<ATSSelectItem> _countryItems = new ATSSelectItemList<ATSSelectItem>(currentLangId);
        
        ATSSelectItem countryItem;
        for(HesCountries country : _countries){
            countryItem = new ATSSelectItem(currentLangId);
            countryItem.setValue(country.getCountryNo());
            countryItem.setLabelPrim(country.getCountryName());
            countryItem.setLabelSec(country.getCountryNameS());
            
            _countryItems.add(countryItem);
        }
        
        setCountries(_countryItems);
    }

    private void populateCities() {
        short currentLangId = FacesUtil.getLocaleLangId();
        
        List<HesCities> _cities = lookupsServicesLocal.getAllCities();
        ATSSelectItemList<ATSSelectItem> _cityItems = new ATSSelectItemList<ATSSelectItem>(currentLangId);
        
        ATSSelectItem cityItem;
        for(HesCities city : _cities){
            cityItem = new ATSSelectItem(currentLangId);
            cityItem.setValue(city.getCityNo());
            cityItem.setLabelPrim(city.getCityName());
            cityItem.setLabelSec(city.getCityNameS());
            
            _cityItems.add(cityItem);
        }
        
        setCities(_cityItems);        
    }

    
    
    /**
     * @return the countries
     */
    public ATSSelectItemList<ATSSelectItem> getCountries() {
        return countries;
    }

    /**
     * @param countries the countries to set
     */
    public void setCountries(ATSSelectItemList<ATSSelectItem> countries) {
        this.countries = countries;
    }

    /**
     * @return the cities
     */
    public ATSSelectItemList<ATSSelectItem> getCities() {
        return cities;
    }

    /**
     * @param cities the cities to set
     */
    public void setCities(ATSSelectItemList<ATSSelectItem> cities) {
        this.cities = cities;
    }
}

